class Observer {
  constructor (data) {
    this.walk(data)
  }

  walk(data) {
    // 判断data是否是对象
    if(!data || typeof data !== 'object') return 
    Object.keys(data).forEach(key => {
      this.defineReactive(data, key, data[key])
    })
  }

  defineReactive (obj, key, val) {
    const _this = this
    const dep = new Dep()
    // 如果val是对象，则把val内部属性也转换成响应式的
    this.walk(val)
    Object.defineProperty(obj, key, {
      enumerable:true,
      configurable:true,
      get () {
        Dep.target && dep.addSub(Dep.target)
        return val
      },
      set (newVal) {
        if (newVal === val) return
        val = newVal
        _this.walk(newVal)
        dep.notify()
      }
    })
  }
}